package com.xiaoge.system.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.xiaoge.auth.annotation.ControllerEndpoint;
import com.xiaoge.response.Result;
import com.xiaoge.system.entity.Department;
import com.xiaoge.system.entity.Role;
import com.xiaoge.system.entity.User;
import com.xiaoge.system.entity.UserRole;
import com.xiaoge.system.mapper.UserMapper;
import com.xiaoge.system.mapper.UserRoleMapper;
import com.xiaoge.system.service.DepartmentService;
import com.xiaoge.system.vo.DeptVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoge
 * @since 2021-02-02
 */
@Api(value = "部门管理")
@RestController
@RequestMapping("/department")
//允许跨域
@CrossOrigin
public class DepartmentController {

    private final DepartmentService departmentService;

    public DepartmentController(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    /**
     * 查询部门人数
     * @return
     */
    @ApiOperation(value = "查询部门人数")
    @GetMapping("/findDeptAndCount")
    public Result findDeptAndCount(){
        List<Department> departments = departmentService.findDeptAndCount();
        if (departments.size()==0){
            throw new Error("未查询到部门信息");
        }
        return Result.ok().data("departments",departments);
    }

    /**
     * 查询部门
     * @return
     */
    @ApiOperation(value = "查询部门")
    @PostMapping("/getDeptList")
    public Result getDeptList(@RequestBody DeptVo deptVo){
        QueryWrapper<Department>queryWrapper=getWrapper(deptVo);
        List<Department> departments = departmentService.getDeptAndCount(queryWrapper);
        return Result.ok().data("departments",departments);
    }

    /**
     * 修改部门
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加部门失败", operation = "添加部门失败")
    @ApiOperation(value = "添加部门")
    @PostMapping("/addDept")
    public Result addDept(@RequestBody Department department){
        return departmentService.addDept(department);
    }

    /**
     * 修改部门
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "修改部门失败", operation = "修改部门")
    @ApiOperation(value = "修改部门")
    @PutMapping("/updateDept")
    public Result updateDept(@RequestBody Department department){
        return departmentService.updateDept(department);
    }

    /**
     * 删除部门
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除部门失败", operation = "删除部门")
    @ApiOperation(value = "删除部门")
    @DeleteMapping("/deleteDept")
    public Result deleteDept(Long id){
        return departmentService.deleteDept(id);
    }

    /**
     * 导出所有部门的excel表格
     *
     * @param response
     * @throws IOException
     */
    @ControllerEndpoint(exceptionMessage = "导出部门excel失败", operation = "导出部门excel")
    @ApiOperation(value = "导出部门excel", notes = "导出所有部门的excel表格")
    @PostMapping("/excel")
    public void export(HttpServletResponse response) throws IOException {
        QueryWrapper<Department>queryWrapper=new QueryWrapper<>();
        List<Department> departments = departmentService.getDeptAndCount(queryWrapper);
        ExportParams params = new ExportParams("部门表", "部门表", ExcelType.HSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params, Department.class, departments);
        ServletOutputStream outputStream = null;
        try {
            response.setHeader("content-type", "application/octet-stream");
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("角色表.xls", "UTF-8"));
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private QueryWrapper<Department> getWrapper(DeptVo deptVo) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        if (deptVo != null) {
            //查询每个条件
            if (!ObjectUtils.isEmpty(deptVo.getDeptName())) {
                queryWrapper.like("name", deptVo.getDeptName());
            }
        }
        return queryWrapper;
    }
}
